package com.navi.loaddata.utils;


import com.navi.loaddata.core.mysqlEntity.MBisDataD;
import com.navi.loaddata.core.repository.impl.MySqlRepositoryImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Transactional
public class GetTimeUtil {

    @Autowired
    private MySqlRepositoryImpl dao;

    public String[] getNowTime() {
        MBisDataD bisData = dao.list(MBisDataD.class,"from MBisDataD where dataCate = ?","NOWL").get(0);
        String[] times = new String[2];
        if (StringUtils.isBlank(bisData.getExt1())) {
            String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            bisData.setExt1(now);
            dao.update(bisData);
        }
        String nowTime = bisData.getExt1();
        String befor1M = LocalDateTime.of(Integer.valueOf(nowTime.substring(0,4)),Integer.valueOf(nowTime.substring(5,7)),
                Integer.valueOf(nowTime.substring(8,10)),Integer.valueOf(nowTime.substring(11,13)),
                Integer.valueOf(nowTime.substring(14,16)),Integer.valueOf(nowTime.substring(17,19)))
                .minusMinutes(5L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        times[0] = nowTime;
        times[1] = befor1M;
        return times;
    }

    public void setNowTime() {
        MBisDataD bisData = dao.list(MBisDataD.class,"from MBisDataD where dataCate = ?","NOWL").get(0);
        String oldTime = bisData.getExt1();
        String plusTime = LocalDateTime.of(Integer.valueOf(oldTime.substring(0,4)),Integer.valueOf(oldTime.substring(5,7)),
                Integer.valueOf(oldTime.substring(8,10)),Integer.valueOf(oldTime.substring(11,13)),
                Integer.valueOf(oldTime.substring(14,16)),Integer.valueOf(oldTime.substring(17,19)))
                .plusMinutes(5L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        bisData.setExt1(plusTime);
        dao.update(bisData);
    }

    public String[] getEtlTime() {
        MBisDataD bisData = dao.list(MBisDataD.class,"from MBisDataD where dataCate = ?","ETLT").get(0);
        String[] times = new String[2];
        if (StringUtils.isBlank(bisData.getExt1())) {
            String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            bisData.setExt1(now);
            dao.update(bisData);
        }
        String nowTime = bisData.getExt1();
        String befor1M = LocalDateTime.of(Integer.valueOf(nowTime.substring(0,4)),Integer.valueOf(nowTime.substring(5,7)),
                Integer.valueOf(nowTime.substring(8,10)),Integer.valueOf(nowTime.substring(11,13)),
                Integer.valueOf(nowTime.substring(14,16)),Integer.valueOf(nowTime.substring(17,19)))
                .minusMinutes(5L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        times[0] = nowTime;
        times[1] = befor1M;
        return times;
    }

    public void setEtlTime() {
        MBisDataD bisData = dao.list(MBisDataD.class,"from MBisDataD where dataCate = ?","ETLT").get(0);
        String oldTime = bisData.getExt1();
        String plusTime = LocalDateTime.of(Integer.valueOf(oldTime.substring(0,4)),Integer.valueOf(oldTime.substring(5,7)),
                Integer.valueOf(oldTime.substring(8,10)),Integer.valueOf(oldTime.substring(11,13)),
                Integer.valueOf(oldTime.substring(14,16)),Integer.valueOf(oldTime.substring(17,19)))
                .plusMinutes(5L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        bisData.setExt1(plusTime);
        dao.update(bisData);
    }
}
